import {ref} from 'vue'
import { defineStore } from 'pinia'
import { getJoinedGroup } from "@/api/group.js";
import { userStore } from "@/stores/userStore.js";
export const useGroupStore = defineStore('groupStore', () => {
    const isInitialized = ref(false)

    const tabList = ref([
        {name: '欢迎', to: "/home/Welcome", isDefault: true, icon: ['fas', 'bolt']},
        {name: '好友', to: "/home/friend", isDefault: true, icon: ['fas', 'user-group']},
    ])

    const updateGroup = (list) => {
        tabList.value.push(list)
    }

    const restoreList = () => {
        tabList.value = [
            {name: '欢迎', to: "/home/Welcome", isDefault: true, icon: ['fas', 'bolt']},
            {name: '好友', to: "/home/friends", isDefault: true, icon: ['fas', 'user-group']},
        ]
    }

    const setGroupList = async () => {
        if (!isInitialized.value) {
            const res = await getJoinedGroup(userStore().userInfo.id)
            if (res.status === 200) {
                isInitialized.value = true
                res.data.forEach((val) => {
                    tabList.value.push({
                        name: val.group_name,
                        to: `/home/groupChat/${val.group_id}`,
                        isDefault: false,
                        icon: val.group_pic
                    })
                })
            }
        }
    }

    return { tabList, updateGroup, restoreList, setGroupList }
})
